<template>
  <div class="ant-pro-page-container-children-content">
    <pro-table
      ref="table"
      toolbar-title="测试单表"
      :row-key="rowKey"
      :request="tableRequest"
      :columns="columns"
    >
      <!-- 查询表单 -->
      <template #search-form="searchFormState">
        <a-row :gutter="16">
          <a-col :md="8" :sm="24">
            <a-form-item label="ID">
              <a-input v-model="searchFormState.queryParam.id" placeholder="" />
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item :wrapper-col="{flex: '1 1 0'}" class="search-actions-wrapper">
              <a-space>
                <a-button type="primary" :loading="searchFormState.loading" @click="searchFormState.reloadTable(true)">
                  查询
                </a-button>
                <a-button @click="searchFormState.resetSearchForm">重置</a-button>
              </a-space>
            </a-form-item>
          </a-col>
        </a-row>
      </template>

      <!-- 操作按钮区域 -->
      <template #toolbar-action>
        <a-button
          v-has="'demo:demo:add'"
          type="primary"
          icon="plus"
          @click="handleAdd()"
        >新建
        </a-button>
      </template>

      <!--表格展示相关插槽-->
      <template #action-slot="text, record">
        <a v-has="'demo:demo:edit'" @click="handleEdit(record)">编辑</a>
        <a-divider type="vertical" />
        <a-popconfirm
          v-has="'demo:demo:del'"
          title="确认要删除吗？"
          @confirm="() => handleDel(record)"
        >
          <a href="javascript:" class="ballcat-text-danger">删除</a>
        </a-popconfirm>
      </template>
    </pro-table>

    <!--表单弹窗-->
    <demo-modal-form ref="formModal" @reload-page-table="reloadPageTable" />
  </div>
</template>

<script>
import ProTable from '@/components/Table/ProTable.js'
import { doRequest } from '@/utils/request'
import { getPage, delObj } from '@/api/demo/demo'
import DemoModalForm from '@/views/demo/demo/DemoModalForm'

export default {
  name: 'DemoPage',
  components: { ProTable, DemoModalForm },
  data () {
    return {
      rowKey: 'id',
      tableRequest: getPage,

      columns: [
        {
          title: '#',
          dataIndex: 'id'
        },
        {
          title: '名称',
          dataIndex: 'name'
        },
        {
          title: '类型',
          dataIndex: 'type'
        },
        {
          title: '备注',
          dataIndex: 'info'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          width: '150px',
          sorter: true
        },
        {
          title: '操作',
          key: 'operate',
          align: 'center',
          width: '150px',
          scopedSlots: { customRender: 'action-slot' }
        }
      ]
    }
  },
  methods: {
    /**
     *  刷新表格
     */
    reloadPageTable (withFirstPage = true) {
      this.$refs.table.reloadTable(withFirstPage)
    },
    /**
     * 新建
     */
    handleAdd () {
      const attributes = { title: '新建' }
      this.$refs.formModal.add(attributes)
    },
    /**
     * 编辑
     * @param record 当前属性
     */
    handleEdit (record) {
      const attributes = { title: '编辑' }
      this.$refs.formModal.update(record, attributes)
    },
    /**
     * 删除
     * @param record 当前属性
     */
    handleDel (record) {
      doRequest(delObj(record[this.rowKey]), {
        onSuccess: () => {
          this.reloadPageTable(false)
        }
      })
    }
  }
}
</script>
